package com.ruoyi.manager.service.impl;

import java.util.List;

import com.ruoyi.manager.mapper.PlaceStrategyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.ruoyi.manager.mapper.PlaceMapper;
import com.ruoyi.manager.domain.Place;
import com.ruoyi.manager.service.IPlaceService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 场地管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-12
 */
@Service
public class PlaceServiceImpl implements IPlaceService 
{
    @Autowired
    private PlaceMapper placeMapper;

    @Autowired
    private PlaceStrategyMapper placeStrategyMapper;

    /**
     * 查询场地管理
     * 
     * @param placeId 场地管理主键
     * @return 场地管理
     */
    @Override
    @Cacheable(cacheNames = "placeCache" , key = "#placeId")
    public Place selectPlaceById(Integer placeId)
    {
        Place place = placeMapper.selectPlaceById(placeId);
        //获取策略id
        Integer strategyId = placeStrategyMapper.selectstrategyIdByPlaceId(placeId);
        place.setStrategyId(strategyId);
        return place;
    }

    /**
     * 查询场地管理列表
     * 
     * @param place 场地管理
     * @return 场地管理
     */
    @Override
    public List<Place> selectPlaceList(Place place)
    {
        List<Place> places = placeMapper.selectPlaceList(place);
        for (Place place1 : places) {
            //获取策略id
            Integer strategyId = placeStrategyMapper.selectstrategyIdByPlaceId(place1.getId());
            place1.setStrategyId(strategyId);
        }
        return  places;
    }

    /**
     * 新增场地管理
     * 
     * @param place 场地管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertPlace(Place place)
    {
        //取出策略id
        //插入策略 场地关联表
        Integer strategyId = place.getStrategyId();
        //主键回显
        int res = placeMapper.insertPlace(place);
        placeStrategyMapper.insert(strategyId , place.getId());

        return res;
    }

    /**
     * 修改场地管理
     * 
     * @param place 场地管理
     * @return 结果
     */
    @Override
    @Transactional
    @CacheEvict(cacheNames = "placeCache" , key = "#place.id")
    public int updatePlace(Place place)
    {
        int res = placeMapper.updatePlace(place);
        //根据场地id修改关联表
        int exist = placeStrategyMapper.selectSumByPlaceId(place.getId());
        if (exist == 0) {
            placeStrategyMapper.insert(place.getStrategyId() , place.getId());
            return res;
        }

        placeStrategyMapper.updateByPlaceId(place.getStrategyId() , place.getId());
        return res;
    }

    /**
     * 批量删除场地管理
     * 
     * @param ids 需要删除的场地管理主键
     * @return 结果
     */
    @Override
    @CacheEvict(cacheNames = "placeCache" , allEntries = true)
    public int deletePlaceByIds(Long[] ids)
    {
        return placeMapper.deletePlaceByIds(ids);
    }

    /**
     * 删除场地管理信息
     * 
     * @param id 场地管理主键
     * @return 结果
     */
    @Override
    @CacheEvict(cacheNames = "placeCache" , allEntries = true)
    public int deletePlaceById(Long id)
    {
        return placeMapper.deletePlaceById(id);
    }
}
